#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    buildUi();
    buildSignalSlots();

    ui->toolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    this->setCentralWidget(ui->textEdit);
}

MainWindow::~MainWindow()
{
    if (ui) {
        delete ui;
        ui = nullptr;
    }
}

void MainWindow::buildUi()
{
    labFile = new QLabel(this);
    labFile->setMinimumWidth(50);
    labFile->setText("文件名");
    ui->statusbar->addWidget(labFile);

    progressBar = new QProgressBar(this);
    progressBar->setMinimumWidth(200);
    progressBar->setMinimum(5);
    progressBar->setMaximum(50);
    progressBar->setValue(ui->textEdit->font().pointSize());
    ui->statusbar->addWidget(progressBar);

    labInfo = new QLabel(this);
    labInfo->setText("字体名称: ");
    ui->statusbar->addPermanentWidget(labInfo);

    actionGroup = new QActionGroup(this);
    actionGroup->addAction(ui->actLang_CNS);
    actionGroup->addAction(ui->actLang_EN);
    actionGroup->setExclusive(true);
    ui->actLang_CNS->setChecked(true);

    spinFontSize = new QSpinBox(this);
    spinFontSize->setMinimum(5);
    spinFontSize->setMaximum(50);
    spinFontSize->setValue(ui->textEdit->font().pointSize());
    spinFontSize->setMinimumWidth(50);
    ui->toolBar->addWidget(spinFontSize);

    fontComboBox = new QFontComboBox(this);
    fontComboBox->setMinimumWidth(150);
    ui->toolBar->addWidget(fontComboBox);

    ui->toolBar->addSeparator();
    ui->toolBar->addAction(ui->actClose);
}

void MainWindow::buildSignalSlots()
{
    connect(spinFontSize, SIGNAL(valueChanged(int)),
            this, SLOT(do_fontSize_changed(int)));

    connect(fontComboBox, &QFontComboBox::currentFontChanged,
            this, &MainWindow::do_fontSelected);
}

void MainWindow::do_fontSize_changed(int fontSize)
{
    QTextCharFormat fmt = ui->textEdit->currentCharFormat();
    fmt.setFontPointSize(fontSize);
    ui->textEdit->mergeCurrentCharFormat(fmt);
    progressBar->setValue(fontSize);
}

void MainWindow::do_fontSelected(const QFont &font)
{
    labInfo->setText("字体名字 : " + font.family());
    QTextCharFormat fmt = ui->textEdit->currentCharFormat();
    fmt.setFont(font);
    ui->textEdit->mergeCurrentCharFormat(fmt);
}

void MainWindow::on_textEdit_copyAvailable(bool b)
{
    ui->actEdit_Cut->setEnabled(b);
    ui->actEdit_Copy->setEnabled(b);
    ui->actEdit_Paste->setEnabled(ui->textEdit->canPaste());
}

void MainWindow::on_textEdit_selectionChanged()
{
    QTextCharFormat fmt = ui->textEdit->currentCharFormat();
    ui->actFont_Bold->setChecked(fmt.font().bold());
    ui->actFont_Italic->setChecked(fmt.font().italic());
    ui->actFont_UnderLine->setChecked(fmt.font().underline());
}

void MainWindow::on_actFont_Bold_triggered(bool checked)
{
    QTextCharFormat fmt = ui->textEdit->currentCharFormat();
    if (checked) {
        fmt.setFontWeight(QFont::Bold);
    } else {
        fmt.setFontWeight(QFont::Normal);
    }
    ui->textEdit->mergeCurrentCharFormat(fmt);
}

void MainWindow::on_actFont_Italic_triggered(bool checked)
{
    QTextCharFormat fmt = ui->textEdit->currentCharFormat();
    fmt.setFontItalic(checked);
    ui->textEdit->mergeCurrentCharFormat(fmt);
}

void MainWindow::on_actFont_UnderLine_triggered(bool checked)
{
    QTextCharFormat fmt = ui->textEdit->currentCharFormat();
    fmt.setFontUnderline(checked);
    ui->textEdit->mergeCurrentCharFormat(fmt);
}

void MainWindow::on_actSys_ToggleText_triggered(bool checked)
{
    if (checked) {
        ui->toolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    } else {
        ui->toolBar->setToolButtonStyle(Qt::ToolButtonIconOnly);
    }
}

void MainWindow::on_actFile_New_triggered()
{
    ui->textEdit->clear();
    ui->textEdit->document()->setModified(false);
    labFile->setText("新建了一个文件");
}

void MainWindow::on_actFile_Open_triggered()
{
    labFile->setText("打开的文件");
}

void MainWindow::on_actFile_Save_triggered()
{
    ui->textEdit->document()->setModified(false);
    labFile->setText("文件已保存");
}

